package com.ifacebox.software.updater;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.concurrent.*;

import javax.swing.*;

/**
 * @author znn
 */
public class SoftwareUpdaterExceptionDialog extends JDialog implements ActionListener {
    private static final long serialVersionUID = 1L;
    private static final int CORE_POOL_SIZE = 1;
    private static final int AUTO_UPDATER_ONE_SECOND = 1;
    private static final int AUTO_UPDATER_SECOND = 5;
    private static final String WAIT_MESSAGE = "下载失败，【%s】秒后开始继续下载...";
    private int autoUpdaterShowSecond = AUTO_UPDATER_SECOND;
    private JButton softwareUpdaterShow, softwareUpdaterDismiss;
    private JLabel softwareUpdaterTitle, softwareUpdaterMessage;
    private final SoftwareUpdaterFrame softwareUpdaterFrame;
    private final ScheduledExecutorService scheduledExecutorService;
    private ScheduledFuture<?> scheduledFuture = null;

    public SoftwareUpdaterExceptionDialog(SoftwareUpdaterFrame softwareUpdaterFrame) {
        super(softwareUpdaterFrame, softwareUpdaterFrame.getTitle(), true);
        super.setIconImage(softwareUpdaterFrame.getIconImage());
        super.setResizable(false);
        super.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        super.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                softwareUpdaterDismiss.doClick();
            }
        });
        super.add(new JPanel() {
            private static final long serialVersionUID = 1L;

            {
                this.setLayout(new BorderLayout());
                this.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
                this.add(softwareUpdaterTitle = new JLabel(String.format(WAIT_MESSAGE, autoUpdaterShowSecond)), BorderLayout.NORTH);
                this.add(softwareUpdaterMessage = new JLabel(" "), BorderLayout.CENTER);
                this.add(new JPanel() {
                    private static final long serialVersionUID = 1L;

                    {
                        this.setLayout(new FlowLayout());
                        this.add(softwareUpdaterShow = new JButton("  是(Y)  "));
                        this.add(softwareUpdaterDismiss = new JButton("  否(N)  "));
                    }
                }, BorderLayout.SOUTH);
            }
        });
        this.softwareUpdaterTitle.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
        this.softwareUpdaterMessage.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
        this.softwareUpdaterShow.setMnemonic('Y');
        this.softwareUpdaterShow.addActionListener(this);
        this.softwareUpdaterDismiss.setMnemonic('N');
        this.softwareUpdaterDismiss.addActionListener(this);
        this.softwareUpdaterFrame = softwareUpdaterFrame;
        this.scheduledExecutorService = new ScheduledThreadPoolExecutor(CORE_POOL_SIZE, r -> new Thread(r, "ShowSoftwareUpdater-Scheduled"));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.softwareUpdaterShow) {
            this.dismiss();
            this.softwareUpdaterFrame.showSoftwareUpdater();
        }
        if (e.getSource() == this.softwareUpdaterDismiss) {
            this.dismiss();
        }
    }


    public void show(String message) {
        super.setTitle("下载失败，是否继续下载？");
        this.softwareUpdaterShow.requestFocus();
        this.softwareUpdaterMessage.setText(message);
        this.softwareUpdaterTitle.setText(String.format(WAIT_MESSAGE, this.autoUpdaterShowSecond = AUTO_UPDATER_SECOND));
        this.scheduledFuture = this.scheduledExecutorService.scheduleWithFixedDelay(() -> {
            if (this.autoUpdaterShowSecond < AUTO_UPDATER_ONE_SECOND) {
                this.scheduledFuture.cancel(false);
                this.softwareUpdaterShow.doClick();
            } else {
                this.softwareUpdaterTitle.setText(String.format(WAIT_MESSAGE, this.autoUpdaterShowSecond));
                this.autoUpdaterShowSecond = this.autoUpdaterShowSecond - AUTO_UPDATER_ONE_SECOND;
            }
        }, AUTO_UPDATER_ONE_SECOND, AUTO_UPDATER_ONE_SECOND, TimeUnit.SECONDS);
        super.pack();
        super.setLocationRelativeTo(this.softwareUpdaterFrame);
        super.setVisible(true);
    }

    public void dismiss() {
        if (this.scheduledFuture != null) {
            this.scheduledFuture.cancel(false);
        }
        super.setVisible(false);
    }

}
